package lmteam.commentservice;

import java.util.*;
import java.lang.String;

import lmteam.documentdataservice.*;


//CommentService - This service handles the details of approving/rejecting
//comments for a specific document.

public class CommentServiceProcessor
   implements ICommentAccess 
{

	//The Comment Service accepts comments from users with the correct
	//authority for the given document and applies them to the document
	//directly, or else to other comments.  Grades and votes are considered
	//special cases of comments.

	CommentServiceProcessor()
	{
		
	}
	
	   public boolean provideComment(lmteam.common.Document in0)
			 throws java.rmi.RemoteException 
	   {

				String filename;
				lmteam.common.User[] reviewer;
        lmteam.common.User approver;
        lmteam.common.Comment[] comment;
				int grade;

			  String originalAuthor;
				lmteam.common.User[] originalReviewer;
        lmteam.common.User originalApprover;
        lmteam.common.Comment[] originalComment;
				int originalGrade;

				filename = in0.getFilename();
        reviewer = in0.getReviewer();
				approver = in0.getApprover();
        comment = in0.getComments();
        grade = in0.getGrade(); 

				DocumentDataProcessor ddp = new DocumentDataProcessor();
			 	lmteam.common.Document originalDocument;
				originalDocument = ddp.get(filename);
				boolean ok;

				originalAuthor = originalDocument.getAuthor();
        originalReviewer = originalDocument.getReviewer();
				originalApprover = originalDocument.getApprover();
        originalComment = originalDocument.getComments();
        originalGrade = originalDocument.getGrade(); 

				//Need to authenticate comments and send to DocDataR by
 						//update(filename, comment)
				if (comment == null)
				{
					return false;
				}

			  if( reviewer == null && approver == null)
				{
					return false;
				}	

				if (originalComment != comment)
				{
					if(String.valueOf(approver) == originalAuthor )
					{
						return false;
					}
					
					for (int i = 0; i < reviewer.length - 1 ; i++)
					{
						if(String.valueOf(reviewer[i]) == originalAuthor)
						{
							return false;
						}
					}
					return true;
				}
     return true;
     }
}
